<?php
/**
 * @file $HeadURL: eventclass.php $
 * @author $Author: DongJC (dongjianchang@haibao.com) $
 * @date $Date: 2015-3-19 6:51:57 $
 * @brief 
 */
namespace haibao\cms\data;

use \haibao\cms\model\filter\EventCategoryDefine as EventCategoryDefineFilter;
use \haibao\cms\model\filter\EventCategory as EventCategoryFilter;

class EventCategoryDefine extends \haibao\cms\data\BaseMysql{
    
	public function __construct(){
		parent::__construct ( '\haibao\cms\model\data\EventCategoryDefine' );
	}
	
	public function addEventClass($model){
		$this->beginTranscation();
		try {
			$this->add($model);
			$this->addEventClassMap($model->Id,$model->ParentId);		
		}catch(\Exception $e ){
			$this->rollback ();
			throw $e;
		}
		$this->endTranscation();
	}
	
	public function updateEventClass($model){
		$this->beginTranscation();
		try{
			$sql="update EventCategoryDefine set CategoryName = %s,ParentId = %s,Alias = %s where Id = %s ";
			$this->query($sql,array($model->CategoryName,$model->ParentId,$model->Alias,$model->Id));
			
			$sql = 'delete from EventCategoryMap where Id in (
						select Id from
							(
								select map.Id from EventCategoryMap map inner join (
									select * from
									(
										select a.ParentId from EventCategoryMap a where a.CategoryId = %s and a.ParentId <> %s
									) a
									join
									(
										select b.CategoryId from EventCategoryMap b where b.ParentId = %s
									) b
								) tmp ON map.ParentId = tmp.ParentId and map.CategoryId = tmp.CategoryId
							) a
						)';
			$this->query($sql, array_pad(array(), 3, $model->Id));
			
			$sql = 'insert into EventCategoryMap (CategoryId, ParentId) select b.CategoryId,a.ParentId from
						(
							select ParentId from EventCategoryMap where CategoryId = %s
						) a join (
							select CategoryId from EventCategoryMap where ParentId = %s
						) b';
			$this->query($sql, array($model->ParentId, $model->Id));
			
		}catch(\Exception $e){
			$this->rollback ();
			throw $e;
		}
		$this->endTranscation();
	}

	public function deleteEventClass($classId){
		$this->beginTranscation();
		try{
			$sql="delete from EventCategoryDefine where Id = %s";
			$this->query($sql,array($classId));
			$sql="delete from EventCategoryMap where CategoryId = %s";
			$this->query($sql,array($classId));
		}catch(\Exception $e){
			$this->rollback ();
			throw $e;
		}
		$this->endTranscation();
	}
	
	public function getEventClassArr($parentId=0){
		$classArr = array();
		$sql="select * from EventCategoryDefine where ParentId = $parentId";
		$result = $this->query($sql);
		while($result->fetch_assoc()){
			$classArr[$result->getData('Id')] = $result->getData('CategoryName');
		}
		return $classArr;
	}
	
	public function getCategoryTree(){
		$arrTree = array();
		$allDict = $this->getAllCategorysDict();
		foreach($allDict as $val){
			if(isset($allDict[$val[EventCategoryDefineFilter::CONDITION_FIELD_NAME_PARENTID]])){
				// 不使用分类id作为key值，避免json_encode对key做排序影响分类显示顺序
				$allDict[$val[EventCategoryDefineFilter::CONDITION_FIELD_NAME_PARENTID]]['children'][] = &$allDict[$val[EventCategoryDefineFilter::CONDITION_FIELD_NAME_ID]];
			}else{
				$arrTree[$val[EventCategoryDefineFilter::CONDITION_FIELD_NAME_ID]] = &$allDict[$val[EventCategoryDefineFilter::CONDITION_FIELD_NAME_ID]];
			}
		}
		return $arrTree;
	}
	
	public function getAllCategorysDict(){
		$query = new EventCategoryDefineFilter();		
		$models = $this->getAll($query);
	
		$retArr = array();
		while (count($models)){
			$model = array_shift($models);
			$retArr[$model->Id] = $model->toArray();
		}
		return $retArr;
	}
	
	public function getCategoryById($id){
		return $this->getOneById($id);
	}
	
	/**
	 * 查找所有孩子节点
	 */
	public function getAllChildrenCategory($categoryId,$categoryAllDict = null){
		static $childArr = array();
		if(!$categoryAllDict){
			$categoryAllDict = $this->getAllCategorysDict();
		}
		foreach($categoryAllDict as $key=>$dict){
			if($dict[EventCategoryDefineFilter::CONDITION_FIELD_NAME_PARENTID] == $categoryId){
				array_push($childArr, $dict);
				$childArr = $this->getAllChildrenCategory($dict[EventCategoryDefineFilter::CONDITION_FIELD_NAME_ID] ,$categoryAllDict);
			}
		}
		return $childArr;
	}
	
	public function getCategoryByEventId($eventId){
		$categoryData = array();
		$smallCategoryData =array();
		$data = array();
		$eventCategoryFilter = new EventCategoryFilter();
		$eventCategoryDefineFilter = new EventCategoryDefineFilter();
		$eventCategoryFilter->select(
			array(
					EventCategoryFilter::CONDITION_FIELD_NAME_CATEGORY_ID,
					EventCategoryFilter::CONDITION_FIELD_NAME_EVENT_ID
			)
		);
		$eventCategoryDefineFilter->select(array(
				EventCategoryDefineFilter::CONDITION_FIELD_NAME_ID,
				EventCategoryDefineFilter::CONDITION_FIELD_NAME_CATEGORY_NAME
		));
		$eventCategoryFilter->leftJoin($eventCategoryDefineFilter,null,EventCategoryFilter::CONDITION_FIELD_NAME_CATEGORY_ID,EventCategoryDefineFilter::CONDITION_FIELD_NAME_ID,EventCategoryFilter::CONDITION_FIELD_NAME_CATEGORY_DEFINE,false);
		$eventCategoryFilter->where(EventCategoryFilter::CONDITION_FIELD_NAME_EVENT_ID,\haibao\frame\data\query\Condition::CONDITION_EQUAL, $eventId);
		$CategoryData = $this->getAll($eventCategoryFilter);
		return $CategoryData;
	}
	
	
	
	private function addEventClassMap($classId,$parentId){
		$this->beginTranscation();
		try{
			$sql="insert into EventCategoryMap (CategoryId,ParentId) values (%s,%s)";
			$this->query($sql,array($classId,$classId));
			
			$sql="insert into EventCategoryMap (CategoryId,ParentId)
			select $classId as CategoryId,ParentId from EventCategoryMap where CategoryId = $parentId ";
			$this->query($sql);
		}catch(\Exception $e){
			$this->rollback ();
			throw $e;
		}
		$this->endTranscation();
	}
	
}